package edu.berkeley.cs160.prog1;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;



public class EmotionCaptureActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finalcapture);
        Button angryButton = (Button) findViewById(R.id.angrytext);
        Button sadButton = (Button) findViewById(R.id.sadtext);
        Button joyousButton = (Button) findViewById(R.id.joyoustext);
        Button happyButton = (Button) findViewById(R.id.happytext);
        Button calmButton = (Button) findViewById(R.id.calmtext);
        Button surprisedButton = (Button) findViewById(R.id.surprisedtext);
        Button continueButton = (Button) findViewById(R.id.continuebutton); 
        angryButton.setOnClickListener((android.view.View.OnClickListener) this);
        sadButton.setOnClickListener((android.view.View.OnClickListener) this);
        joyousButton.setOnClickListener((android.view.View.OnClickListener) this);
        happyButton.setOnClickListener((android.view.View.OnClickListener) this);
        calmButton.setOnClickListener((android.view.View.OnClickListener) this);
        surprisedButton.setOnClickListener((android.view.View.OnClickListener) this);
        continueButton.setOnClickListener((android.view.View.OnClickListener) this);
    }
    
    private static final int CAMERA_PIC_REQUEST = 1337;
    private Uri fileUri;
    public static final int ANGRY_PIC = 1;
    public static final int SAD_PIC = 2;
    public static final int JOYOUS_PIC = 3;
    public static final int HAPPY_PIC = 4;
    public static final int CALM_PIC = 5;
    public static final int SURPRISED_PIC = 6;
    
    public void onClick (View v) {
    	
    	switch (v.getId()) {
    	case R.id.continuebutton:
    		Intent continueIntent = new Intent(v.getContext(), LevelSelectionActivity.class);
    		this.startActivity(continueIntent);
    		break;
    	case R.id.angrytext:
    	
            // create Intent to take a picture and return control to the calling application
            Intent getAngryPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            //fileUri = getOutputMediaFileUri(ANGRY_PIC); // create a file to save the image
            //getAngryPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

            // start the image capture Intent
            startActivityForResult(getAngryPic, ANGRY_PIC); 
            
    		break;
    		
    		
    	case R.id.sadtext:
        	Intent getSadPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //fileUri = getOutputMediaFileUri(SAD_PIC); 
            //getSadPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(getSadPic, SAD_PIC); 
    		break;
    	case R.id.joyoustext:
        	Intent getJoyousPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //fileUri = getOutputMediaFileUri(JOYOUS_PIC); 
            //getJoyousPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(getJoyousPic, JOYOUS_PIC); 
    		break;
    	case R.id.happytext:
        	Intent getHappyPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //fileUri = getOutputMediaFileUri(HAPPY_PIC); 
            //getHappyPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(getHappyPic, HAPPY_PIC); 
    		break;
    	case R.id.calmtext:
        	Intent getCalmPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //fileUri = getOutputMediaFileUri(CALM_PIC); 
            //getCalmPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(getCalmPic, CALM_PIC); 
    		break;
    	case R.id.surprisedtext:
        	Intent getSurprisedPic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //fileUri = getOutputMediaFileUri(SURPRISED_PIC); 
            //getSurprisedPic.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
            startActivityForResult(getSurprisedPic, SURPRISED_PIC); 
    		break;	
    	}
    
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK) {
    			// Image captured and saved to fileUri specified in the Intent
               //Toast.makeText(this, "Image saved to:\n" +
                  //       data.getData(), Toast.LENGTH_LONG).show();
    		
    			Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    			ImageView image = null;
    			switch (requestCode) {
    			case ANGRY_PIC:
    				image = (ImageView) findViewById(R.id.angry);
    				break;
    			case SAD_PIC:
    				image = (ImageView) findViewById(R.id.sad);
    				break;
    			case JOYOUS_PIC:
    				image = (ImageView) findViewById(R.id.joyous);
    				break;
    			case HAPPY_PIC:
    				image = (ImageView) findViewById(R.id.happy);
    				break;
    			case CALM_PIC:
    				image = (ImageView) findViewById(R.id.calm);
    				break;
    			case SURPRISED_PIC:
    				image = (ImageView) findViewById(R.id.surprised);
    				break;
    			}
        		image.setImageBitmap(thumbnail);
        } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
        } else {
                // Image capture failed, advise user
        }
    		
    }
    


    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        File mediaFile = null;
        switch (type) {
        case ANGRY_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "ANGRY_PIC" + ".jpg");
        case SAD_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "SAD_PIC" + ".jpg");
        case JOYOUS_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "JOYOUS_PIC" + ".jpg");
        case HAPPY_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "HAPPY_PIC" + ".jpg");
        case CALM_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "CALM_PIC" + ".jpg");
        case SURPRISED_PIC:
        	mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "SURPRISED_PIC" + ".jpg");
        }
        return mediaFile;
    }

}
